package com.wwcom.darteagle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.wwcom.darteagle.data.DartEagleDatabase;
import com.wwcom.darteagle.model.GlobalAppData;
import com.wwcom.darteagle.model.MatchLegs;
import com.wwcom.darteagle.model.MatchPlayer;
import com.wwcom.darteagle.model.MatchSets;
import com.wwcom.darteagle.util.Settings;
import com.wwcom.darteagle.util.Settings.MatchSettings;


public class MatchSetupSettingsActivity extends Activity {
	private DartEagleDatabase db;
	private final GlobalAppData gd= GlobalAppData.getSingletonInstance();
	
    /** Called when the activity is first created. */	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

	    //layout
        setContentView(R.layout.matchsetup_settings);                
		
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	if (db == null)
    		db= new DartEagleDatabase(this);             	

    	//check if we have a valid gd.matchInfo - if we don't we can't continue here
    	//we were likely removed from memory so deeply that the global data didn't survive
    	if (gd.matchInfo == null) {
    		finish(); //go back to front page
    		return;
    	}
    	//after this we are guarenteed to have a valid gd.matchInfo unless we are in onPause

        ((TextView)findViewById(R.id.match_description)).setText(gd.matchInfo.getMatchDescription());
		
        //setup from match 
        ((CheckBox)findViewById(R.id.double_out)).setChecked(gd.matchInfo.isDoubleOut());
        ((CheckBox)findViewById(R.id.double_in)).setChecked(gd.matchInfo.isDoubleIn());
        ((CheckBox)findViewById(R.id.treble_out)).setChecked(gd.matchInfo.isTrebleOut());
        ((CheckBox)findViewById(R.id.treble_in)).setChecked(gd.matchInfo.isTrebleIn());
        ((Spinner)findViewById(R.id.matchsetup_legs)).setSelection(gd.matchInfo.getNumLegs()-1);
      	((Spinner)findViewById(R.id.matchsetup_sets)).setSelection(gd.matchInfo.getNumSets());
       	((Spinner)findViewById(R.id.matchsetup_legtarget)).setSelection((gd.matchInfo.getLegTarget() - 101) / 100);
       	if (gd.matchInfo.isLoserStarts())
       		((Spinner)findViewById(R.id.matchsetup_startplayer)).setSelection(1);
        
        ((Spinner)findViewById(R.id.matchsetup_legs)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { 
        		gd.matchInfo.setNumLegs(position+1);
                ((TextView)findViewById(R.id.match_description)).setText(gd.matchInfo.getMatchDescription());
            } 
            public void onNothingSelected(AdapterView<?> adapterView) {
                return;
            } 
        });        
           	
        ((Spinner)findViewById(R.id.matchsetup_sets)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { 
        		gd.matchInfo.setNumSets(position);
                ((TextView)findViewById(R.id.match_description)).setText(gd.matchInfo.getMatchDescription());
            } 
            public void onNothingSelected(AdapterView<?> adapterView) {
                return;
            } 
        });        

        ((Spinner)findViewById(R.id.matchsetup_legtarget)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { 
        		gd.matchInfo.setLegTarget((position+1)*100 + 1); 
                ((TextView)findViewById(R.id.match_description)).setText(gd.matchInfo.getMatchDescription());
            } 
            public void onNothingSelected(AdapterView<?> adapterView) {
                return;
            } 
        });     
        
        ((Spinner)findViewById(R.id.matchsetup_startplayer)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { 
        		gd.matchInfo.setLoserStarts(position == 1); 
                ((TextView)findViewById(R.id.match_description)).setText(gd.matchInfo.getMatchDescription());
            } 
            public void onNothingSelected(AdapterView<?> adapterView) {
                return;
            } 
        });     

        ((CheckBox)findViewById(R.id.double_in)).setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		boolean isChecked= ((CheckBox)v).isChecked();
        		gd.matchInfo.setDoubleIn(isChecked);
        		if (isChecked) {
        			((CheckBox)findViewById(R.id.treble_in)).setChecked(false); 
        			gd.matchInfo.setTrebleIn(false);
        		}
                ((TextView)findViewById(R.id.match_description)).setText(gd.matchInfo.getMatchDescription());
        	}
        });
        ((CheckBox)findViewById(R.id.treble_in)).setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		boolean isChecked= ((CheckBox)v).isChecked();
        		gd.matchInfo.setTrebleIn(isChecked);
        		if (isChecked) {
        			((CheckBox)findViewById(R.id.double_in)).setChecked(false);   
        			gd.matchInfo.setDoubleIn(false);        			
        		}
                ((TextView)findViewById(R.id.match_description)).setText(gd.matchInfo.getMatchDescription());
        	}
        });

        ((CheckBox)findViewById(R.id.double_out)).setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		boolean isChecked= ((CheckBox)v).isChecked();
        		gd.matchInfo.setDoubleOut(isChecked);
        		if (isChecked) {
        			((CheckBox)findViewById(R.id.treble_out)).setChecked(false);        			
        			gd.matchInfo.setTrebleOut(false);
        		}
                ((TextView)findViewById(R.id.match_description)).setText(gd.matchInfo.getMatchDescription());
        	}
        });
        ((CheckBox)findViewById(R.id.treble_out)).setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		boolean isChecked= ((CheckBox)v).isChecked();
        		gd.matchInfo.setTrebleOut(isChecked);
        		if (isChecked) {
        			((CheckBox)findViewById(R.id.double_out)).setChecked(false);        			
        			gd.matchInfo.setDoubleOut(false);        			
        		}
                ((TextView)findViewById(R.id.match_description)).setText(gd.matchInfo.getMatchDescription());
        	}
        });
    	
    }    
    @Override
    public void onPause() {
    	super.onPause();
    	db.close();
    	db= null;    	
    }
    
    	
    //buttons        
	public void onClickStartMatch(View v) {					
    	GlobalAppData gd= GlobalAppData.getSingletonInstance();
		if (gd.matchInfo.getMatchPlayers().size() < 1) {
			Toast t = Toast.makeText(this, "Add at least one player before starting your match", Toast.LENGTH_SHORT);
			t.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
			t.show();
			return;
		}
    	
		if (gd.matchInfo.getNumSets() > 0)
			gd.match= new MatchSets(gd.matchInfo);
		else
			gd.match= new MatchLegs(gd.matchInfo);
		
		gd.match.startPlaying();    
		
		MatchSettings.updateSettings(gd.match);
		Settings.saveSettings(this);
		
		for (MatchPlayer mp : gd.match.getMatchPlayers()) {
			db.addPlayer(mp.getPlayer());
		}					
				
		Intent i= new Intent(this,MatchActivity.class);
	    startActivity(i);
	    finish();
	}
	
    @Override
    public void onBackPressed() {
		Intent i= new Intent(this,MatchSetupPlayersActivity.class);
	    startActivity(i);
	    finish();
    }
    
	public void onClickPlayerSetup(View v) {					
		Intent i= new Intent(this,MatchSetupPlayersActivity.class);
	    startActivity(i);
		finish();
	}	

    //menus
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.darteagle_options_menu, menu);
        return true;
    }    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.menu_options:
        	Intent i= new Intent(this,OptionsActivity.class);
        	startActivity(i);
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
}